package com.xhd.base.scheduler;

import java.util.Map;
import java.util.Vector;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.base.util.xBaseObj;
import com.xhd.config.SchedulerConfig;
import com.xhd.exception.SystemException;
import com.xhd.exception.XException;
import com.xhd.servlet.ClientAction;

public class SchedulerQueue {
	  private Vector jm187 = new Vector(128);
	  private Class jm188 = null;
	  private static SchedulerQueue jm16;
	  private static final Log jm9;
	  static Class jm10;

	  static
	  {
	    Class tmp3_0 = jm10;
	    if (tmp3_0 == null)
	    {
	      
	      try
	      {
	        SchedulerQueue.jm10 = Class.forName("com.sinocc.base.scheduler.SchedulerQueue");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp3_0);
	  }

	  public static synchronized SchedulerQueue getInstance()
	  {
	    if (jm16 == null)
	    {
	      jm16 = new SchedulerQueue();
	      jm16.jm188 = SchedulerConfig.getInstance().getTaskClass();
	      try
	      {
	        SchedulerTask localSchedulerTask = (SchedulerTask)jm16.jm188.newInstance();
	        localSchedulerTask.stopOldTasks();
	      }
	      catch (Exception localException)
	      {
	        jm9.error(localException);
	      }
	    }
	    return jm16;
	  }

	  public synchronized SchedulerTask popTask()
	  {
	    if (this.jm187.size() > 0)
	    {
	      Item localItem = (Item)this.jm187.remove(0);
	      if (localItem != null)
	        return localItem.Task;
	    }
	    return null;
	  }

	  public void pushTask(xBaseObj paramBusinessClass, ClientAction paramClientAction)
	    throws XException
	  {
	    SchedulerTask localSchedulerTask = null;
	    try
	    {
	      localSchedulerTask = (SchedulerTask)this.jm188.newInstance();
	    }
	    catch (Exception localException)
	    {
	      jm9.error(localException);
	      throw new SystemException(localException.getMessage());
	    }
	    localSchedulerTask.setClientAction(paramClientAction);
	    localSchedulerTask.setParent(paramBusinessClass);
	    localSchedulerTask.onPushQueue(paramBusinessClass);
	    jm185(new Item(localSchedulerTask));
	    SchedulerProcessThread.wakeUpAll();
	  }

	  private synchronized void jm185(Item paramItem)
	  {
	    int i = 0;
	    for (int j = this.jm187.size() - 1; j >= 0; --j)
	    {
	      if (!(paramItem.isPriority((Item)this.jm187.get(j))))
	        continue;
	      i = j + 1;
	      break;
	    }
	    this.jm187.add(i, paramItem);
	  }

	  public void setPriority(SchedulerTask paramSchedulerTask)
	  {
	    if (paramSchedulerTask == null)
	      return;
	    jm186(paramSchedulerTask.getSchTaskId(), paramSchedulerTask.getPriority());
	  }

	  public void setPriority(Vector paramVector)
	  {
	    int i = paramVector.size();
	    for (int j = 0; j < i; ++j)
	    {
	      Object localObject1 = paramVector.get(j);
	      Object localObject2;
	      if (localObject1 instanceof SchedulerTask)
	      {
	        localObject2 = (SchedulerTask)localObject1;
	        jm186(((SchedulerTask)localObject2).getSchTaskId(), ((SchedulerTask)localObject2).getPriority());
	      }
	      else
	      {
	        if (!(localObject1 instanceof Map))
	          continue;
	        localObject2 = (Map)localObject1;
	        jm186(Integer.parseInt((String)((Map)localObject2).get("schtaskid")), Integer.parseInt((String)((Map)localObject2).get("priority")));
	      }
	    }
	  }

	  private void jm186(int paramInt1, int paramInt2)
	  {
	    if (paramInt1 == 0)
	      return;
	    int i = this.jm187.indexOf(new Integer(paramInt1));
	    if (i < 0)
	      return;
	    Item localItem = (Item)this.jm187.remove(i);
	    if (localItem == null)
	      return;
	    localItem.Task.setPriority(paramInt2);
	    jm185(localItem);
	  }

	  public boolean remove(SchedulerTask paramSchedulerTask)
	  {
	    return this.jm187.remove(new Item(paramSchedulerTask));
	  }

	  private class Item
	  {
	    public SchedulerTask Task;

	    public Item(SchedulerTask paramSchedulerTask)
	    {
	      this.Task = paramSchedulerTask;
	    }

	    public boolean equals(Object paramObject)
	    {
	      if (paramObject == null)
	        return false;
	      if (paramObject instanceof Integer)
	        return (this.Task.getSchTaskId() == ((Integer)paramObject).intValue());
	      if (paramObject instanceof SchedulerTask)
	        return (this.Task.getSchTaskId() == ((SchedulerTask)paramObject).getSchTaskId());
	      return false;
	    }

	    public boolean isPriority(Item paramItem)
	    {
	      if (this.Task.getPriority() > paramItem.Task.getPriority())
	        return true;
	      if (this.Task.getPriority() == paramItem.Task.getPriority())
	        return (this.Task.getSchTaskId() < paramItem.Task.getSchTaskId());
	      return false;
	    }
	  }

}
